package org.lql.algo.codecrush.hot100.dynamicProgramming;

/**
 * @author: liangqinglong
 * @date: 2025-09-14 09:24
 * @description: 152. 乘积最大子数组 <a href="https://leetcode.cn/problems/maximum-product-subarray/description/?envType=study-plan-v2&envId=top-100-liked">...</a>
 **/
public class MaxProduct {

	public int maxProduct(int[] nums) {
		int n = nums.length;
		int[] max = new int[n];
		int[] min = new int[n];
		max[0] = nums[0];
		min[0] = nums[0];
		for (int i = 1; i < n; i++) {
			max[i] = Math.max(max[i - 1] * nums[i], Math.max(min[i - 1] * nums[i], nums[i]));
			min[i] = Math.min(max[i - 1] * nums[i], Math.min(min[i - 1] * nums[i], nums[i]));
		}
		int ans = max[0];
		for (int i = 1; i < n; i++) {
			ans = Math.max(ans, max[i]);
		}
		return ans;
	}

	public static void main(String[] args) {
		MaxProduct maxProduct = new MaxProduct();
		System.out.println(maxProduct.maxProduct(new int[]{2, 3, -2, 4}));
	}
}
